package com.lei.service.impl;

import com.lei.dao.RukuDao;
import com.lei.dao.impl.RukuDaoImpl;
import com.lei.entity.Ck;
import com.lei.entity.LayDate;
import com.lei.entity.Ruku;
import com.lei.service.RukuService;
import com.lei.util.PageBean;

import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/22 17:13
 */
public class RukuServiceImpl implements RukuService {
    private final RukuDao rukuDao = new RukuDaoImpl();

    @Override
    public String addRuku(List<Map> tableData, Map map) {
        // 添加入库单之后返回的主键
        int rukuid = rukuDao.addRuku(map);
        if (rukuid > 0) {
            // 添加入库详情表
            int result = rukuDao.addRukuDetail(tableData, rukuid);
            if (result > 0) {
                // 增加商品的数量,返回影响的总行数
                int i = rukuDao.rukuAndAddBookCount(tableData);
                return i + "";
            }
        }
        return "0";
    }

    @Override
    public PageBean<Ruku> getAllRuku(Ruku ruku) {
        PageBean<Ruku> page = new PageBean<>();
        long count = rukuDao.getCount(ruku);
        if (count > 0) {
            page.setCount(count);
            List<Ruku> allRuku = rukuDao.getAllRuku(ruku);
            page.setData(allRuku);
        }
        return page;
    }

    @Override
    public PageBean getAllRukuDetailByRukuId(String rukuid) {
        PageBean<Map> page = new PageBean<>();
        page.setCount((long) 0);
        List<Map> list = rukuDao.getAllRukuDetailByRukuId(rukuid);
        if (list.size() > 0) {
            page.setData(list);
        }
        return page;
    }

    @Override
    public String addCk(List<Map> tableData, Map map) {
        // 添加入库单之后返回的主键
        int ckid = rukuDao.addCk(map);
        if (ckid > 0) {
            // 添加入库详情表
            int result = rukuDao.addCkDetail(tableData, ckid);
            if (result > 0) {
                // 增加商品的数量,返回影响的总行数
                int i = rukuDao.ckAndUpdateBookCount(tableData);
                return i + "";
            }
        }
        return "0";
    }

    @Override
    public PageBean<Ck> getAllCk(Ck ck) {
        PageBean<Ck> page = new PageBean<>();
        long count = rukuDao.getCountCk(ck);
        if (count > 0) {
            page.setCount(count);
            List<Ck> allRuku = rukuDao.getAllCk(ck);
            page.setData(allRuku);
        }
        return page;
    }

    @Override
    public PageBean getAllCkDetailByCkId(String ckid) {
        PageBean<Map> page = new PageBean<>();
        page.setCount((long) 0);
        List<Map> list = rukuDao.getAllCkDetailByCkId(ckid);
        if (list.size() > 0) {
            page.setData(list);
        }
        return page;
    }

    @Override
    public Map<String, List> getCkAndRuKuMoney(LayDate layDate) {
        Map<String, List> map = rukuDao.getCkAndRuKuMoney(layDate);
        return map;
    }
}
